package com.jingyanzi.sys.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingyanzi.sys.dao.SysBusinessKeyMapper;
import com.jingyanzi.sys.dao.SysBusinessTypeMapper;
import com.jingyanzi.sys.domain.BusinessKeyDO;
import com.jingyanzi.sys.domain.BusinessTypeDO;
import com.jingyanzi.sys.domain.query.BusinessKeyQueryDO;
import com.jingyanzi.sys.domain.query.BusinessTypeQueryDO;
import com.jingyanzi.sys.domain.result.PageAll;
import com.jingyanzi.sys.domain.result.ResultDO;
import com.jingyanzi.sys.domain.result.ResultSupport;
import com.jingyanzi.sys.domain.service.IBusinessService;
import com.jingyanzi.sys.entity.SysBusinessKey;
import com.jingyanzi.sys.entity.SysBusinessKeyExample;
import com.jingyanzi.sys.entity.SysBusinessType;
import com.jingyanzi.sys.entity.SysBusinessTypeExample;
import com.jingyanzi.sys.entity.SysBusinessTypeExample.Criteria;
import com.jingyanzi.sys.util.BeanUtilsExtends;

@Service("iBusinessService")
public class BusinessServiceImpl extends BaseServiceImpl implements IBusinessService {
	
	@Autowired
	private SysBusinessKeyMapper businessKeyMapper;
	
	@Autowired
	private SysBusinessTypeMapper businessTypeMapper;

	@Override
	public ResultDO addType(BusinessTypeDO businessTypeDO) {
		ResultDO result=new ResultSupport(true);
		
		if(businessTypeDO==null) {
			result.setSuccess(false);
			result.setErrorMsg("添加失败");
			return result;
		}
		try {
			SysBusinessType businessType=new SysBusinessType();
			BeanUtilsExtends.copyProperties(businessType, businessTypeDO);
			//判断父借点
			if(businessType.getBusinessId()==null||businessType.getBusinessId()==0) {
				businessType.setBusinessId(0);
			}
			else {
				//如果又父节点  那么修改曾经父节点
				SysBusinessType selectByPrimaryKey = businessTypeMapper.selectByPrimaryKey(businessType.getBusinessId());
				if(selectByPrimaryKey.getIsNode()==1) {
					selectByPrimaryKey.setIsNode(0);
					selectByPrimaryKey.setModifi(businessType.getAuthor());
					selectByPrimaryKey.setModifitime(new Date());
					businessTypeMapper.updateByPrimaryKeySelective(selectByPrimaryKey);
				}
			}
			businessType.setCreatetime(new Date());
			businessType.setModifitime(new Date());
			businessType.setIsDelete(1);
			businessType.setIsNode(1);
			businessType.setStatus(1);
			int insert = businessTypeMapper.insert(businessType);
			
			if(insert==0) {
				result.setSuccess(false);
				result.setErrorMsg("添加失败1");
				return result;
			}
			
			result.setModel(ResultDO.FIRST_MODEL_KEY, businessType.getId());
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
			result.setSuccess(false);
			result.setErrorMsg("有异常");
			return result;
			
		}
		return result;
	}

	@Override
	public ResultDO removeType(Integer id,Integer toid,String name) {
		
		ResultDO result=new ResultSupport(true);
		if(id==null||id==0) {
			result.setSuccess(false);
			result.setErrorMsg("ID不正确");
			
			return result;
		}
		
		Date date=new Date();
		try {
			//查询出所有的该ID下面的所有
			SysBusinessTypeExample example=new SysBusinessTypeExample();
			
			Criteria createCriteria = example.createCriteria();
			createCriteria.andBusinessIdEqualTo(id);
			createCriteria.andIsDeleteEqualTo(1);
			List<SysBusinessType> selectByExample = businessTypeMapper.selectByExample(example);
			
			if(selectByExample!=null) {
				
				if(toid==null||toid==0) {
					toid=0;
				}
				else {
					
					SysBusinessType selectByPrimaryKey = businessTypeMapper.selectByPrimaryKey(toid);
					
					if(selectByPrimaryKey==null) {
						throw new Exception();
					}
					
					if(selectByPrimaryKey.getIsNode()==1) {
						selectByPrimaryKey.setIsNode(0);
						selectByPrimaryKey.setModifi(name);
						int updateByPrimaryKeySelective = businessTypeMapper.updateByPrimaryKeySelective(selectByPrimaryKey);
					
						if(updateByPrimaryKeySelective==0) {
							throw new Exception();
						}
					}
				}
				for(SysBusinessType s:selectByExample) {
					//修改下面所有的
					s.setBusinessId(toid);
					s.setModifitime(date);
					s.setModifi(name);
					int updateByPrimaryKeySelective = businessTypeMapper.updateByPrimaryKeySelective(s);
					
					if(updateByPrimaryKeySelective==0) {
						throw new Exception();
					}
				}
			}
			SysBusinessType selectByPrimaryKey = businessTypeMapper.selectByPrimaryKey(id);
			
			selectByPrimaryKey.setModifitime(date);
			selectByPrimaryKey.setModifi(name);
			selectByPrimaryKey.setIsDelete(-1);
			selectByPrimaryKey.setStatus(0);
			
			int updateByPrimaryKeySelective = businessTypeMapper.updateByPrimaryKeySelective(selectByPrimaryKey);
		
			if(updateByPrimaryKeySelective==0) {
				throw new Exception();
			}
			
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			result.setSuccess(false);
			result.setErrorMsg("修改异常");
			return result;
		}
		
		result.setModel(ResultDO.FIRST_MODEL_KEY, id);
		
		return result;
	}

	@Override
	public ResultDO modifiType(BusinessTypeDO businessTypeDO) {
		
		ResultDO result=new ResultSupport(true);
		
		if(businessTypeDO==null||businessTypeDO.getId()==null||businessTypeDO.getId()==0) {
			result.setSuccess(false);
			result.setErrorMsg("为空");
			return result;
		}
		Date date =new Date();
		try {
			SysBusinessType sysBusinessType=new SysBusinessType();
			
			if(businessTypeDO.getBusinessId()!=null&&businessTypeDO.getBusinessId()!=0) {
				//查询自己以前的信息
				SysBusinessType selectByPrimaryKey = businessTypeMapper.selectByPrimaryKey(businessTypeDO.getId());
				if(selectByPrimaryKey.getBusinessId()!=businessTypeDO.getBusinessId()) {
					//判断以前的ID父亲ID是否只有他一个
					SysBusinessTypeExample example=new SysBusinessTypeExample();
					Criteria createCriteria = example.createCriteria();
					createCriteria.andBusinessIdEqualTo(selectByPrimaryKey.getBusinessId());
					createCriteria.andIsDeleteEqualTo(1);
					createCriteria.andIdNotEqualTo(selectByPrimaryKey.getId());
					
					//修改以前的父节点
					List<SysBusinessType> selectByExample = businessTypeMapper.selectByExample(example);
					
					if(selectByExample==null||selectByExample.size()==0) {
						SysBusinessType businessType=new SysBusinessType();
						businessType.setId(selectByPrimaryKey.getBusinessId());
						businessType.setModifi(businessTypeDO.getModifi());
						businessType.setModifitime(date);
						businessType.setIsNode(1);
						int updateByPrimaryKeySelective = businessTypeMapper.updateByPrimaryKeySelective(businessType);
						if(updateByPrimaryKeySelective==0) {
							throw new Exception();
						}
					}
					//新的父节点
					SysBusinessType selectByPrimaryKey2 = businessTypeMapper.selectByPrimaryKey(businessTypeDO.getBusinessId());
					if(selectByPrimaryKey2==null) {
						result.setSuccess(false);
						result.setErrorMsg("选择的父ID不存在");
						throw new Exception();
					}
					//如果以前父节点没人
					if(selectByPrimaryKey2.getIsNode()==1) {
						selectByPrimaryKey2.setIsNode(0);
						selectByPrimaryKey2.setModifitime(date);
						selectByPrimaryKey2.setModifi(businessTypeDO.getModifi());
						int updateByPrimaryKeySelective = businessTypeMapper.updateByPrimaryKeySelective(selectByPrimaryKey2);
						if(updateByPrimaryKeySelective==0) {
							throw new Exception();
						}
					}
				}
			}
			
			
			//修改自己信息
			BeanUtilsExtends.copyProperties(sysBusinessType, businessTypeDO);
			sysBusinessType.setModifitime(date);
			int updateByPrimaryKeySelective = businessTypeMapper.updateByPrimaryKeySelective(sysBusinessType);
			
			if(updateByPrimaryKeySelective==0) {
				
				throw new Exception();
			}
			
			
		} catch (Exception e) {
			
			result.setSuccess(false);
			return result;
			// TODO: handle exception
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, businessTypeDO.getId());
		return result;
	}

	@Override
	public ResultDO getType(Integer id) {
		ResultDO result=new ResultSupport(true);
		
		if(id==null||id==0) {
			result.setSuccess(false);
			result.setErrorMsg("id错误");
			return result;
		}
		SysBusinessType selectByPrimaryKey = businessTypeMapper.selectByPrimaryKey(id);
		
		if(selectByPrimaryKey!=null) {
			
			BusinessTypeDO businessType=new BusinessTypeDO();
			BeanUtilsExtends.copyProperties(businessType, selectByPrimaryKey);
			result.setModel(ResultDO.FIRST_MODEL_KEY, businessType);
			return result;
		}
		result.setSuccess(false);
		result.setErrorMsg("未找到");
		return result;
		
	}
	@Override
	public ResultDO queryType(BusinessTypeQueryDO queryDO,PageAll page) {
		
		ResultDO result=new ResultSupport(true);
		
		SysBusinessTypeExample example=new SysBusinessTypeExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andIsDeleteEqualTo(1);
		if(queryDO!=null) {
			if(queryDO.getName()!=null&&"".equals(queryDO.getName())) {
				createCriteria.andNameLike("%"+queryDO.getName()+"%");
			}
		}
		
		PageHelper.startPage(page.getPageindex(), page.getPagesize());
		List<SysBusinessType> selectByExample = businessTypeMapper.selectByExample(example);
		PageInfo<SysBusinessType> pageinfo=new PageInfo<SysBusinessType>(selectByExample);
		
		page.setTotal((int) pageinfo.getTotal());
		List<SysBusinessType> list = pageinfo.getList();
		List<BusinessTypeDO> list1=new ArrayList<BusinessTypeDO>();
		for(SysBusinessType s:list) {
			BusinessTypeDO b1=new BusinessTypeDO();
			BeanUtilsExtends.copyProperties(b1, s);
			list1.add(b1);
		}
		
		result.setModel(ResultDO.FIRST_MODEL_KEY, list1);
		result.setModel("page", page);
		return result;
	}
	
	@Override
	public ResultDO showSelect(Integer id) {
		
		ResultDO result=new ResultSupport(false);
		if(id==null) {
			result.setSuccess(false);
			result.setErrorMsg("ID为空");
			return result;
		}
		String recursion = recursion(id);
		
		System.out.println(recursion);
		String[] split = recursion.split(",");
		List<Integer> list=new ArrayList<Integer>();
		if(split!=null&&split.length!=0) {
			for(String s:split) {
				
				if("".equals(s)) {
					
				}
				else {
					
					list.add(Integer.parseInt(s));
				}
			}
		}
		list.add(id);
		if(list.size()!=0) {
			SysBusinessTypeExample example=new SysBusinessTypeExample();
			Criteria createCriteria = example.createCriteria();
			createCriteria.andIsDeleteEqualTo(1);
			createCriteria.andIdNotIn(list);
			List<SysBusinessType> selectByExample = businessTypeMapper.selectByExample(example);
			
			result.setSuccess(true);
			result.setModel(ResultDO.FIRST_MODEL_KEY, selectByExample);
			
		}
		return result;
	}
	
	//递归获取
	public String recursion(int i) {
		
		SysBusinessTypeExample example=new SysBusinessTypeExample();
		example.createCriteria().andBusinessIdEqualTo(i);
		List<SysBusinessType> list = businessTypeMapper.selectByExample(example);
		
		String x="";
		
		if(list==null||list.size()==0) {
			
			return "";
		}
		else {
			for(SysBusinessType p:list) {
				x=x+","+p.getId()+","+recursion(p.getId());
			}
		}
		return x;
	}

	@Override
	public ResultDO addKey(BusinessKeyDO feedbackDO) {
		
		ResultDO result=new ResultSupport(false);
		
		if(feedbackDO==null||feedbackDO.getBusinessId()==null||feedbackDO.getId()!=0) {
			result.setSuccess(false);
			result.setErrorMsg("不能未空");
		}
		Date date=new Date();
		
		SysBusinessKey  sysBusinessKey=new SysBusinessKey();
		
		BeanUtilsExtends.copyProperties(sysBusinessKey, feedbackDO);
		
		sysBusinessKey.setCreatetime(date);
		sysBusinessKey.setModifitime(date);
		sysBusinessKey.setIsDelete(1);
		sysBusinessKey.setStatus(1);
		
		int insert = businessKeyMapper.insert(sysBusinessKey);
		if(insert!=0) {
			result.setSuccess(true);
			result.setModel(ResultDO.FIRST_MODEL_KEY, sysBusinessKey.getId());
			
		}
		return result;
	}

	@Override
	public ResultDO removeKey(Integer id,String name) {
		ResultDO result=new ResultSupport(true);
		if(id==null) {
			result.setSuccess(false);
			result.setErrorMsg("ID为空");
			return result;
		}
		
		SysBusinessKey sysBusiness=new SysBusinessKey();
		
		sysBusiness.setModifi(name);
		sysBusiness.setModifitime(new Date());
		
		try {
			
			sysBusiness.setIsDelete(-1);
			sysBusiness.setStatus(0);
			int updateByPrimaryKeySelective = businessKeyMapper.updateByPrimaryKeySelective(sysBusiness);
			if(updateByPrimaryKeySelective==0) {
				result.setSuccess(false);
				result.setErrorMsg("删除失败");
				return result;
			}
		}catch (Exception e) {
			
			System.out.println(e.getMessage());
			result.setSuccess(false);
			result.setErrorMsg("删除失败");
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, id);
		return result;
	}

	@Override
	public ResultDO modifiKey(BusinessKeyDO feedBackDO) {
		// TODO Auto-generated method stub
		
		ResultDO result=new ResultSupport(true);
		
		if(feedBackDO==null||feedBackDO.getId()==null||feedBackDO.getId()==0) {
			
			result.setSuccess(false);
			result.setErrorMsg("修改失败");
			
			return result;
		}
		
		SysBusinessKey sysBusiness=new SysBusinessKey();
		
		BeanUtilsExtends.copyProperties(sysBusiness, feedBackDO);
		
		sysBusiness.setModifitime(new Date());
		try {
			
			int updateByPrimaryKeySelective = businessKeyMapper.updateByPrimaryKeySelective(sysBusiness);
			if(updateByPrimaryKeySelective==0) {
				result.setSuccess(false);
				result.setErrorMsg("修改失败");
				return result;
			}
			
		}catch (Exception e) {
			// TODO: handle exception
			result.setSuccess(false);
			result.setErrorMsg("修改失败");
			return result;
		}
		result.setSuccess(true);
		result.setModel(ResultDO.FIRST_MODEL_KEY, feedBackDO.getId());
		return result;
	}

	@Override
	public ResultDO getKey(Integer id) {
		ResultDO result=new ResultSupport(true);
		
		if(id==null||id==0) {
			result.setSuccess(false);
			result.setErrorMsg("id错误");
			return result;
		}
		SysBusinessKey selectByPrimaryKey = businessKeyMapper.selectByPrimaryKey(id);
		
		if(selectByPrimaryKey!=null) {
			
			BusinessKeyDO sin=new BusinessKeyDO();
			BeanUtilsExtends.copyProperties(sin, selectByPrimaryKey);
			result.setModel(ResultDO.FIRST_MODEL_KEY, sin);
			return result;
		}
		result.setSuccess(false);
		result.setErrorMsg("未找到");
		return result;
	}
	
	@Override
	public ResultDO queryKey(BusinessKeyQueryDO queryDO,PageAll page) {
		// TODO Auto-generated method stub
		ResultDO result=new ResultSupport(true);
		
		SysBusinessKeyExample  example=new SysBusinessKeyExample();
		com.jingyanzi.sys.entity.SysBusinessKeyExample.Criteria createCriteria = example.createCriteria();
		if(queryDO!=null) {
			if(queryDO.getBusinessId()!=null&&queryDO.getBusinessId()!=0) {
				createCriteria.andBusinessIdEqualTo(queryDO.getBusinessId());
			}
		}
		
		PageHelper.startPage(page.getPageindex(), page.getPagesize());
		List<SysBusinessKey> selectByExample = businessKeyMapper.selectByExample(example);
		PageInfo<SysBusinessKey> pageinfo=new PageInfo<SysBusinessKey>(selectByExample);
		
		page.setTotal((int)pageinfo.getTotal());
		
		List<SysBusinessKey> list = pageinfo.getList();
		
		List<BusinessKeyDO> lists=new ArrayList<BusinessKeyDO>();
		if(list!=null) {
			
			for(SysBusinessKey s:list) {
				BusinessKeyDO bk=new BusinessKeyDO();
				BeanUtilsExtends.copyProperties(bk, s);
				lists.add(bk);
			}
		}
		result.setSuccess(true);
		
		result.setModel("page",page);
		result.setModel(ResultDO.FIRST_MODEL_KEY, lists);
		return result;
	}

}
